﻿using System.IO;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XSerialization;
using XSerializer.UnitTests.Sample;

namespace XSerializer.UnitTests
{
    /// <summary/>
    [TestClass()]
    public class XSerializerTest
    {
        /// <summary/>
        [TestMethod()]
        public void DeserializeLibraryTest()
        {
            // arrange
            XDocument xmlDocument = XDocument.Load(new MemoryStream(Properties.Resources.LibraryXml));
            IXSerializer xSerializer = new XSerialization.XSerializer();

            // act
            ILibrary lib = xSerializer.Deserialize(xmlDocument, typeof (Library)) as ILibrary;

            // assert
            Assert.IsNotNull(lib);
            Assert.AreEqual(1, lib.Books.Count);
            Assert.AreEqual("b0836217462", lib.Books[0].Id);
            Assert.IsTrue((bool)lib.Books[0].Available);
            Assert.AreEqual(1, lib.Books[0].Authors.Count);
            Assert.AreEqual("2000-02-12",lib.Books[0].Authors[0].Dead);
            Assert.AreEqual(4, lib.Books[0].Characters.Count);
            Assert.AreEqual("Snoopy",lib.Books[0].Characters[1].Name);
        }

        /// <summary/>
        [TestMethod()]
        public void SerializeLibraryTest()
        {
            // arrange
            XDocument xmlDocument = XDocument.Load(new MemoryStream(Properties.Resources.LibraryXml));
            IXSerializer xSerializer = new XSerialization.XSerializer();
            ILibrary lib = xSerializer.Deserialize(xmlDocument, typeof(Library)) as ILibrary;

            // act
            lib.Books[0].Id = "Snoopy Book";
            XDocument newDocument = xSerializer.Serialize(lib, "library");

            // Assert
            string newContent = newDocument.ToString();
            string xmlContent = xmlDocument.ToString();
            xmlContent = xmlContent.Replace("b0836217462", "Snoopy Book");

            Assert.AreEqual(newContent, xmlContent);
        }

        /// <summary/>
        [TestMethod()]
        public void SerializeRequiredElementsTest()
        {
            // arrange
            ILibrary library = new Library();
            library.Books.Add(new Book());

            // act
            IXSerializer xSerializer = new XSerialization.XSerializer();
            XDocument xmlDocument = xSerializer.Serialize(library, "library");

            // assert
            Assert.AreEqual(@"<library><book id=""""><isbn>0</isbn><title></title></book></library>", ConvertXmlString(xmlDocument.ToString()));
        }

        /// <summary>
        /// Replace \r\n etc.
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        private static string ConvertXmlString(string xml)
        {
            xml = xml.Replace("\r\n  ", "");
            xml = xml.Replace("\r\n", "");
            xml = xml.Replace("\n", "");
            xml = xml.Replace("\r", "");
            xml = xml.Replace(">  <", "><");
            return xml;
        }
    }
}
